<template>
  <el-dialog :title="$t('tool.modelManage.' + title)" :visible.sync="dialogFormVisible" custom-class="jtyj-dialog"
    :close-on-click-modal="false">
    <el-form :model="form" :rules="rules" ref="form" label-position="top" class="jtyj-form">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item :label="$t('tool.modelManage.chineseName')" prop="name">
            <el-input v-model="form.name" class="jtyj-input"
              :placeholder="$t('tool.others.placehoderInput')"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button class="jtyj-btn-cancel" @click="cancel">{{
        $t("setup.buttonText.cancel")
      }}</el-button>
      <el-button class="jtyj-btn-primary" @click="submitForm">{{
        $t("setup.buttonText.save")
      }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  addSampleTreeApi,
} from "@/api/myspace/sampleDataManagement/index";
import { mapState } from "vuex";
export default {
  props: {
    sampleSetType: {
      type: Number,
      default: () => 1,
    },
  },
  data() {
    return {
      title: null,
      dialogFormVisible: false,
      parentInfo: {},
      form: {
        name: "",
        sampleSetType: 1, // 1为感知样本，2为辅助决策样本
        parentId: null,
        nodeType: 0, // 树类型 0=文件, 1=文件夹
        userId: null,
      },
      rules: {
        name: [{ required: true, message: "名称不可为空", trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapState({
      userId: (state) => state.user.id,
    }),
  },
  mounted() {
    this.form.sampleSetType = this.sampleSetType;
  },
  methods: {
    init(row, node) {
      this.form.sampleSetType = this.sampleSetType;
      this.dialogFormVisible = true;
      this.reset();
      if (!row) {
        this.title = "addModel";
        this.form.parentId = !node ? 0 : node.id;
      } else {
        this.title = "editModel";
      }
    },

    reset() {
      this.form = {
        name: null,
        parentId: null,
        nodeType: 1, // 0表示文件夹，1表示文件
        userId: null,
        sampleSetType: this.sampleSetType, // 1为感知样本，2为辅助决策样本
      };
      this.resetForm("form");
    },
    cancel() {
      this.dialogFormVisible = false;
    },
    // 表单提交
    submitForm() {
      this.form.userId = this.userId;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addSampleTreeApi(this.form).then((response) => {
            if (response.code == 200) {
              this.$message({
                message: "新增成功",
                type: "success",
              });
              const newNodeId = response.data;
              this.$emit("refresh", newNodeId);
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
  },
};
</script>

<style></style>
